import { Component } from "~/utils/enums"

import * as c from "~/utils/components"
import BaseForm from "@admin/entity/base/BaseForm"
import { Rule } from "~/utils/annotate"

export default class User extends BaseForm {
    static postsResolve: (list: any[]) => void
    static rolesResolve: (list: any[]) => void
    userId = 0
    nickName = ''
    deptId?: number
    deptId2?: number
    phonenumber = ''
    email = ''
    userName = ''
    password = ''
    sex = ''
    status = '0'
    postIds: number[] = []
    roleIds: number[] = []
    remark = ''

    clear(): void {
        this.setForm(new User(this.opts))
        this.resetFields()
    }

    getNickName(): c.Input {
        return {
            type: Component.Input,
            mode: 'query',
            required: true,
            label: '用户昵称',
        }
    }

    getDeptId(): c.TreeSelect {
        return {
            type: Component.TreeSelect,
            label: '归属部门',
            config: {
                url: '/system/user/deptTree',
                valueTarget: 'id',
                labelTarget: 'label',
            },
            element: {
                checkStrictly: true
            }
        }
    }


    @Rule("phone")
    getPhonenumber(): c.Input {
        return {
            type: Component.Input,
            mode: 'query',
            label: '手机号码',
            // show: () => this.status === '1'
        }
    }

    @Rule("email")
    getEmail(): c.Input {
        return {
            type: Component.Input,
            label: '邮箱'
        }
    }

    getUserName(): c.Input {
        return {
            type: Component.Input,
            label: '用户名称',
            show: () => this.userId === 0
        }
    }

    getPassword(): c.Input {
        return {
            type: Component.Input,
            label: '用户密码',
            show: () => this.userId === 0
        }
    }

    getSex(): c.DictSelect {
        return {
            type: Component.DictSelect,
            label: '用户性别',
            mode: 'query',
            config: {
                dict: 'sys_user_sex'
            },
            events: {
                loaded: (list) => {
                    console.log('loaded', list);
                },
                change() {
                    console.log('性别变换');
                },
                itemChange(val) {
                    console.log('itemChange', val);
                }
            }
        }
    }

    getStatus(): c.Radio | c.DictSelect {
        if (this.opts?.mode === 'query') {
            return {
                type: Component.DictSelect,
                mode: 'query',
                label: '状态',
                config: {
                    dict: 'sys_normal_disable'
                }
            }
        }
        return {
            type: Component.Radio,
            label: '状态',
            config: {
                dict: 'sys_normal_disable'
            }
        }
    }

    getPostIds(): c.Select {

        return {
            type: Component.Select,
            label: '岗位',
            config: {
                list: []
            },
            events: {
                loaded: (_list, update) => {
                    User.postsResolve = update
                },
            },
            element: {
                multiple: true,
                collapseTags: true
            },
        }
    }

    getRoleIds(): c.Select {
        return {
            type: Component.Select,
            label: '角色',
            config: {
                list: []
            },
            events: {
                loaded: (_list, update) => {
                    console.log('loaded');

                    User.rolesResolve = update
                },
                change: (val) => {
                    console.log('val', val);
                },
                itemChange: (val) => {
                    console.log(val);
                },
                removeTag: (val) => {
                    console.log(val);
                }
            },
            element: {
                multiple: true,
                collapseTags: true
            }
        }
    }

    getRemark(): c.Textarea {
        return {
            type: Component.Textarea,
            label: '备注',
            span: 24
        }
    }
}